package leetcode325;

import java.util.Arrays;
import java.util.function.IntPredicate;

/**
 * @author aodre , QQ : 480029069
 * @date 2022/12/25 14:41
 */
public class leetcode6271差值问题 {


    // 这是错误的思路 ，这样理解就是错的
    public static int maximumTastinessEoor(int[] price, int k) {
        Arrays.sort(price);
        int max = 0;
        int min = 1000000000;
        for(int i = 1;i < price.length;i++){
            min = Math.min(price[i] - price[i = 1],min);
            max = Math.max(max,min);
        }
        return max;
    }
    public static int lastTrue(IntPredicate predicate, int l, int r) {
        assert l <= r;
        while (l != r) {
            int mid = (l + r + 1) / 2;
            if (predicate.test(mid)) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        if (!predicate.test(l)) {
            l--;
        }
        return l;
    }

    public int maximumTastiness(int[] price, int k) {
        Arrays.sort(price);
        int ans = lastTrue((m) -> {
            int seg = 0;
            int last = (int)-1e9;
            for(int x : price) {
                if(x - last >= m) {
                    last = x;
                    seg++;
                }
            }
            return seg >= k;
        }, 0, (int)1e9);
        return ans;
    }

}
